﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace PageOffice6_Net_Core.Controllers.FileMakerConvertPDFs
{
    public class FileMakerConvertPDFsController : Controller
    {

        private readonly IWebHostEnvironment _webHostEnvironment;

        public FileMakerConvertPDFsController(IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment = webHostEnvironment;
        }

        public IActionResult Index()
        {
            string url = "";
            url = _webHostEnvironment.WebRootPath;
            ViewBag.url = url + "\\FileMakerConvertPDFs\\doc";
            return View();
        }


        public void Convert(int id)
        {
			String docName = "doc0" + id + ".doc";
			String pdfName = "doc0" + id + ".pdf";

            PageOfficeNetCore.FileMakerCtrl fileMakerCtrl = new PageOfficeNetCore.FileMakerCtrl(Request);

            //打开文档
            fileMakerCtrl.FillDocumentAsPDF("doc/" + docName, PageOfficeNetCore.DocumentOpenType.Word, pdfName);
            Response.Body.WriteAsync(Encoding.GetEncoding("GB2312").GetBytes(fileMakerCtrl.GetHtml()));
        }

        public async Task<ActionResult> SaveDoc()
        {
            PageOfficeNetCore.FileSaver fs = new PageOfficeNetCore.FileSaver(Request, Response);
            await fs.LoadAsync();
            string webRootPath = _webHostEnvironment.WebRootPath;
            fs.SaveToFile(webRootPath + "/FileMakerConvertPDFs/doc/" + fs.FileName);
            return fs.Close();
        }
		public IActionResult Edit(int id)
		{
			String docName = "doc0" + id + ".doc";

            PageOfficeNetCore.PageOfficeCtrl poCtrl = new PageOfficeNetCore.PageOfficeCtrl(Request);
			//打开Word文档
			poCtrl.WebOpen("doc/" + docName, PageOfficeNetCore.OpenModeType.docNormalEdit, "tom");
			ViewBag.poCtrl = poCtrl.GetHtml();
			return View();
		}

	}
}